//
//  LoginViewController.m
//  shopping
//
//  Created by 徐双 on 2017/9/3.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "MyTabBarController.h"
#import "ForgetPasswordViewController.h"
@interface LoginViewController ()
@property (nonatomic, strong) MyTabBarController *tabBarController;

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UILabel *label1;
@property (weak, nonatomic) IBOutlet UIButton *weiBo;
@property (weak, nonatomic) IBOutlet UIButton *qq;
@property (weak, nonatomic) IBOutlet UIButton *weiXin;


@end

@implementation LoginViewController
- (void)viewWillAppear:(BOOL)animated{
    
    
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.label1.hidden = YES;
    self.weiBo.hidden = YES;
    self.qq.hidden = YES;
    self.weiXin.hidden = YES;
    
    [self leftNavHide];
    

    
    //添加注册按钮
    UIButton *buttonAdd = [[UIButton alloc] init];
    
    buttonAdd.frame = CGRectMake(DEF_SCREEN_WIDTH - 15, 0, 40, 40);
    [buttonAdd setTitle:@"注册" forState:UIControlStateNormal];
    [buttonAdd addTarget:self action:@selector(registerClick) forControlEvents:UIControlEventTouchUpInside];
    UIColor * color = getColor(@"222222");
    [buttonAdd setTitleColor:color forState:UIControlStateNormal];
    buttonAdd.titleLabel.font = [UIFont systemFontOfSize:15];
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:buttonAdd];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    //账号不可以输入中文
    self.phoneTextField.keyboardType = UIKeyboardTypeASCIICapable;
    self.codeTextField.keyboardType = UIKeyboardTypeASCIICapable;
    
    
}
- (IBAction)ForgetClick:(UIButton *)sender {
    
    ForgetPasswordViewController *vc = [[ForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
}
- (IBAction)loginlick:(UIButton *)sender {
    
    
//    UINavigationBar *bar = [UINavigationBar appearance];
//    
//    
//    bar.barStyle=UIBarStyleBlackTranslucent;
//    
//    //设置显示的颜色
//    
//    bar.barTintColor =[ZJCModel colorWithString:@"ffffff"];
//    
//    //设置字体颜色
//    
//    bar.tintColor = [UIColor whiteColor];
//    
//    
//    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//    MyTabBarController *barController = [[MyTabBarController alloc] init];
//    // TAB BAR
//    
//    [self presentViewController:barController animated:YES completion:nil];
    
   
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
    hud.tag = 500;
    
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _phoneTextField.text forKey:@"username"];
    [productParams setValue: _codeTextField.text forKey:@"password"];
//    [productParams setValue: @"18841765036" forKey:@"username"];
//    [productParams setValue: @"111111" forKey:@"password"];
    [[MNetworkHandler sharedInstance] conURL:loginUrl
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue] == 1){
                                       [hud removeFromSuperview];
                                       
                                       NSMutableDictionary *tokenD = [returnData objectForKey:@"data"];
                                       
                                       
                                       [PublicFunction saveConfig:[tokenD objectForKey:@"token"] forKey:@"token"];
                                       
                            
                                       NSLog(@"token == %@",[PublicFunction getConfigWithKey:@"token"]);
                                       
                                       UINavigationBar *bar = [UINavigationBar appearance];
                                       
                                       
//                                       bar.barStyle=UIBarStyleBlack;
                                       [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
                                       //设置显示的颜色
                                       
                                       bar.barTintColor =[ZJCModel colorWithString:@"ffffff"];
                                       
                                       //设置字体颜色
                                       
                                       bar.tintColor = [UIColor whiteColor];
                                       
                                       
                                       [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
                                       MyTabBarController *barController = [[MyTabBarController alloc] init];
                                       // TAB BAR
                                       
                                       [self presentViewController:barController animated:YES completion:nil];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:@"登录失败" duration:1.0f];
                                   }
                                   
                                       
                                   
                                   
                             } failureBlock:^(NSError *error) {
                                 
                                 [hud removeFromSuperview];
                                 NSLog(@"%@",error);
//                                 [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                             }];
//
    
}

-(void)registerClick{

    RegisterViewController * vc = [[RegisterViewController alloc] init];
    
    vc.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:vc animated:YES];

}

@end
